/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ibmmq_analysis_head.c                              */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/08 by jikw                                 */
/*******************************************************************/
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frct_comm.h"
#include "../../../src/module/ibmmq/config.h"
#include "../test_ibmmq_comm.h"
#include <apmtest_comm.h>
#include <frreport_opt.h>
#include <ibmmq/cnaps2.h>
#include <ibmmq/ibmmq_core.h>

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

extern int ibmmq_analysis_head(char *p_data
                      , unsigned int ui_head_len
                      , L7_head_ibmmq **pp_st_head);

static void case1(void)
{
    char *p_data = NULL;
    unsigned int ui_head_len = 10;
    L7_head_ibmmq *p_st_head = NULL;
    int i_ret = FR_SUCC;

    i_ret = ibmmq_analysis_head(p_data, ui_head_len, &p_st_head);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    return;
}

static void case2(void)
{
    char *p_data = (char *)"\x54\x53\x48\x4D\x00\x00\x01\x38\x00\x00\x00\x01\x00\x00\x00\x00\x02\x81\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x01\x00\x00\x65\x05\x00\x00";
    unsigned int ui_head_len = 10;
    int i_ret = FR_SUCC;

    i_ret = ibmmq_analysis_head(p_data, ui_head_len, NULL);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    return;
}

static void case3(void)
{
    char *p_data = (char *)"\x54\x53\x48\x4D\x00\x00\x01\x38\x00\x00\x00\x01\x00\x00\x00\x00\x02\x81\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x01\x00\x00\x65\x05\x00\x00";
    unsigned int ui_head_len = 0;
    L7_head_ibmmq *p_st_head = NULL;
    int i_ret = FR_SUCC;

    i_ret = ibmmq_analysis_head(p_data, ui_head_len, &p_st_head);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    return;
}

static void case4(void)
{
    char *p_data = (char *)"\x54\x53\x48\x4D\x00\x00\x01\x38\x00\x00\x00\x01\x00\x00\x00\x00\x03\x81\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x01\x00\x00\x65\x05\x00\x00";
    unsigned int ui_head_len = 36;
    L7_head_ibmmq *p_st_head = NULL;
    int i_ret = FR_SUCC;

    i_ret = ibmmq_analysis_head(p_data, ui_head_len, &p_st_head);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    return;
}

static void case5(void)
{
    char *p_data = (char *)"\x54\x53\x48\x20\x00\x00\x01\xEC\x01\x85\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x11\x05\x65\x00\x00";
    unsigned int ui_head_len = 28;
    L7_head_ibmmq *p_st_head = NULL;
    int i_ret = FR_SUCC;

    i_ret = ibmmq_analysis_head(p_data, ui_head_len, &p_st_head);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_head->arr_structid, "TSH");
    CU_ASSERT_EQUAL(p_st_head->ui_msg_len, 492);
    CU_ASSERT_EQUAL(p_st_head->u32_byte_order, 1);
    CU_ASSERT_EQUAL(p_st_head->u32_segm_type, 133);
    CU_ASSERT_EQUAL(p_st_head->ui_head_len, 28);
    CU_ASSERT_EQUAL(p_st_head->s_ccsid, 1381);

    frfree(p_st_head);

    return;
}

static void case6(void)
{
    char *p_data = (char *)"\x54\x53\x48\x4D\x00\x00\x01\x38\x00\x00\x00\x01\x00\x00\x00\x00\x02\x81\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x01\x00\x00\x65\x05\x00\x00";
    unsigned int ui_head_len = 36;
    L7_head_ibmmq *p_st_head = NULL;
    int i_ret = FR_SUCC;

    i_ret = ibmmq_analysis_head(p_data, ui_head_len, &p_st_head);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_head->arr_structid, "TSHM");
    CU_ASSERT_EQUAL(p_st_head->ui_msg_len, 312);
    CU_ASSERT_EQUAL(p_st_head->u32_byte_order, 2);
    CU_ASSERT_EQUAL(p_st_head->u32_segm_type, 129);
    CU_ASSERT_EQUAL(p_st_head->ui_head_len, 36);
    CU_ASSERT_EQUAL(p_st_head->s_ccsid, 1381);

    frfree(p_st_head);

    return;
}

int add_suite_ibmmq_analysis_head(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" ibmmq_analysis_head()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL ||
        CU_add_test(pSuite, "case6", case6) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
